/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.entity;

import org.ipti.br.tag.entity.enums.PersonageType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.ipti.br.tag.util.EncryptionUtil;

/**
 *
 * @author israel
 */
@Entity
public class Personage extends AbstractEntity{

    @Column(nullable = false, length = 45)
    private String name;
    
    @Enumerated(EnumType.ORDINAL)
    private PersonageType personageType;

    @ManyToOne
    @JoinColumn(name = "organization_id", nullable = false)
    private Organization organization;

    @ManyToOne
    @JoinColumn(name = "father_id")
    private Personage personageFather;
    
    public Personage() {
    }

    @Override
    public void generateHash(){
        if(getId() == null){
            String personageHashString = "" + this.getName()
                    + this.getPersonageType()
                    + ((this.getOrganization() != null) ? this.getOrganization().getId() : null)
                    + ((this.getPersonageFather() != null) ? this.getPersonageFather().getId() : null);
            this.setId(Long.valueOf(EncryptionUtil.encrypt(personageHashString).hashCode()));
        }
    }

    public Personage(String name, Organization organization, PersonageType personageType, Personage personageFather) {
        this.name = name;
        this.personageType = personageType;
        this.organization = organization;
        this.personageFather = personageFather;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public PersonageType getPersonageType() {
        return personageType;
    }

    public void setPersonageType(PersonageType personageType) {
        this.personageType = personageType;
    }

    public Organization getOrganization() {
        return organization;
    }

    public void setOrganization(Organization organization) {
        this.organization = organization;
    }

    public Personage getPersonageFather() {
        return personageFather;
    }

    public void setPersonageFather(Personage personageFather) {
        this.personageFather = personageFather;
    }
}
